/*
 * @Date: 2019-06-01 20:11:26
 * @Author: Ryan Huang
 * @Description:
 */
package libs

import (
	"bufio"
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"strings"
)

func CatchErr() {
	if err := recover(); err != nil {
		fmt.Println(err)
	}
}

func ReadLine(reader *bufio.Reader) string {
	line, _ := reader.ReadString('\n')
	return strings.Trim(line, "\r\n")
}

func ReadBytes(reader *bufio.Reader) []byte {
	line, _ := reader.ReadBytes('\n')
	return bytes.TrimRight(line, "\r\n")
}

func Md5(s string) string {
	h := md5.New()
	h.Write([]byte(s))
	cipherStr := h.Sum(nil)
	return hex.EncodeToString(cipherStr)
}

func ToJson(i interface{}) []byte {
	str, err := json.Marshal(i)
	if err != nil {
		panic(err)
	}
	return str
}

func GetMapKey(map1 map[string]string) string {
	var getKey string
	for key := range map1 {
		getKey = key
	}
	return getKey
}

func GetMapKeys(map1 map[string]string) []string {
	var getKeys []string
	for key := range map1 {
		getKeys = append(getKeys, key)
	}
	return getKeys
}

func GetValue(v interface{}) interface{} {
	switch v.(type) {
	case int:
		return v.(int)
	case string:
		return v.(string)
	case int64:
		return v.(int64)
	case int8:
		return v.(int8)
	case int16:
		return v.(int16)
	case int32:
		return v.(int32)
	case uint:
		return v.(uint)
	case uint8:
		return v.(uint8)
	case uint16:
		return v.(uint16)
	case uint32:
		return v.(uint32)
	case uint64:
		return v.(uint64)
	case float32:
		return v.(float32)
	case float64:
		return v.(float64)
	}
	return v
}

func Interface2Value(i interface{}, v *interface{}) {
	switch i.(type) {
	case int:
		*v = i.(int)
	case string:
		*v = i.(string)
	case int64:
		*v = i.(int64)
	case int8:
		*v = i.(int8)
	case int16:
		*v = i.(int16)
	case int32:
		*v = i.(int32)
	case uint:
		*v = i.(uint)
	case uint8:
		*v = i.(uint8)
	case uint16:
		*v = i.(uint16)
	case uint32:
		*v = i.(uint32)
	case uint64:
		*v = i.(uint64)
	case float32:
		*v = i.(float32)
	case float64:
		*v = i.(float64)
	}
}
